package com.swufe.team;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ProgressBar;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import java.util.List;

public class RateListActivity extends AppCompatActivity implements AdapterView.OnItemLongClickListener {
    private static final String TAG ="RateListActivity";
    private Handler handler;
    private ProgressBar bar;
    private ArrayAdapter adapter;
    private GridView mylist;
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        SharedPreferences sp = getSharedPreferences("myrate", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY, "");
        Log.i("List","lastRateDateStr=" + logDate);

        setContentView(R.layout.activity_rate_list); //自定义布局
        mylist=findViewById(R.id.mylistview);
        bar = findViewById(R.id.prog);

        mylist.setEmptyView(bar);

        String data[]={"one","two","three"};
        adapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_activated_1,data);
        mylist.setAdapter(adapter);
        mylist.setOnItemLongClickListener(this);


        handler=new Handler(){
            @Override
            public void handleMessage(@NonNull Message msg) {
                //收到数据
                if(msg.what==5){
                    List<String>list= (List<String>) msg.obj;
                    adapter = new ArrayAdapter<String>(RateListActivity.this, android.R.layout.simple_list_item_activated_1,list);
                    mylist.setAdapter(adapter);

                }
                super.handleMessage(msg);
            }
        };
        MyTask myTask=new MyTask();
        myTask.setHandler(handler);
        Thread t=new Thread(myTask);
        t.start();
    }

    @Override
    public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
//        adapter.remove(mylist.getItemAtPosition(position));


        AlertDialog.Builder builder =new AlertDialog.Builder(this);
        builder.setTitle("提示").setMessage("请确认是否删除当前数据").setPositiveButton("是", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                Log.i(TAG, "onClick: 对话框事件处理");
//                adapter.remove(position);
                adapter.notifyDataSetChanged();

            }
        }).setNegativeButton("否",null);
        builder.create().show();

        return  true;
    }

}